<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

include_once APPPATH . '/helpers/session_helper.php';
include_once APPPATH. '/models/constants/flight_constants.php';

class Main extends CI_Controller {

    public function index() {

	$this->main_index();
	/*
	  $this->load->view("header");
	  $this->load->view("main_search");
	  $this->load->view("footer");

	 */
    }

    public function main_index() {


	$jsfiles = array();
	 array_push($jsfiles, "libs/json2.js");
        array_push($jsfiles, "libs/jquery-validate-1.10.0/jquery.validate.min.js");
	// array_push($jsfiles, "libs/require.js-2.1.4/require.min.js");
	array_push($jsfiles, "libs/select2-3.2/select2.min.js");
	array_push($jsfiles, "libs/jquery.blockUI.js");
	array_push($jsfiles, "airports.js");
        array_push($jsfiles,"libs/cookie-util.js");
       
	array_push($jsfiles, "pages/flysearch.js");
        
	//more js file

	$data['js'] = $jsfiles;
	$this->load->view("main_header");
	$this->load->view("fly_search/fly_search");
	$this->load->view("main_footer", $data);
    }

    public function searchFlyRequest() {

	try {

	    $this->load->model("fly_search/fly_search_criteria");
	    $search_criteria = $this->fly_search_criteria->getInstance();
	    $search_criteria->boardingCode = $this->input->post("boardingairpotCode");
	    $search_criteria->landingCode = $this->input->post("landingairpotCode");
	    $search_criteria->godate = $this->input->post("goDate");
	    $search_criteria->returndate = $this->input->post("returnDate");
	    $search_criteria->flydirection = $this->input->post("directionOption");
	    $search_criteria->yetiskinnumber = $this->input->post("yetiskinNumber");
	    $search_criteria->bebeknumber = $this->input->post("bebekNumber");
	    $search_criteria->cocuknumber = $this->input->post("cocukNumber");
	    $search_criteria->dateoption = $this->input->post("dateOption");
	    $search_criteria->cabinclass = $this->input->post("cabinClass");
	    $search_criteria->flighttype = $this->input->post("flightType");
	    $this->load->model("fly_search/fly_search");
	    $this->fly_search->search($search_criteria);
	    $this->load->helper('url');
	    $response = $this->fly_search->getFlightSearchResult();
	    $responseJson = json_encode($response);
	    echo $responseJson;
   
	} catch (Exception $e) {
	    error_log(get_class($e) . " thrown. Message: " . $e->getMessage() . "  in " . $e->getFile() . " on line " . $e->getLine());
	    error_log('Exception trace stack: ' . print_r($e->getTrace(), 1));
	    echo $e->getMessage(). "|".$e->getFile()."|".$e->getLine();
	}
	return;

	/*
	  public $boardingCode;
	  public  $landingCode;
	  public  $godate;
	  public  $returndate;
	  public $dateOption;
	  public $flyDirection;
	  public $yetiskinNumber;
	  public $bebekNumber;
	  public $cocukNumber;

	  boardingairpotCode:IST
	  landingairpotCode:ADA
	  goDate:Sat Mar 09 2013 00:00:00 GMT+0200 (GTB Standart Saati)
	  returnDate:Tue Apr 30 2013 00:00:00 GMT+0300 (GTB Yaz Saati)
	  yetiskinNumber:1
	  cocukNumber:0
	  bebekNumber:0
	  dateOption:1
	 * */
	/*
	  echo json_encode($search_criteria);
	  return;
	 *
	 */
    }

    public function searchResults() {
	$jsfiles = array();
        $cssfiles = array();
        
        array_push($cssfiles,"pages/fly_result.css");
	array_push($jsfiles, "libs/jquery-validate-1.10.0/jquery.validate.min.js");
	//array_push($jsfiles, "libs/require.js-2.1.4/require.min.js");
	array_push($jsfiles, "libs/select2-3.2/select2.min.js");
        array_push($jsfiles, "libs/jquery.blockUI.js");
	array_push($jsfiles, "airports.js");
	array_push($jsfiles, "pages/flyresult.js");
	//array_push($jsfiles, "pages/flysearch.js");
	//more js file
	require APPPATH . '/models/fly_search/travelport_low_search_response.php';
	require APPPATH . '/models/fly_search/air_pricing_solution.php';
	require APPPATH . '/models/fly_search/air_pricing_info.php';
	require APPPATH . '/models/fly_search/air_segment.php';
	require APPPATH . '/models/fly_search/book_info.php';
	require APPPATH . '/models/fly_search/journey.php';
        require APPPATH . '/models/fly_search/combined_air_price_solution.php';
	$this->load->model("fly_search/fly_search_criteria");

	$session = new Session();
	$results = $session->get("travelport_response");
        $combined_air_solutions = $session->get(Fly_Constant::SESSION_COMBINED_AIR_PRICE_SOLUTIONS_PARAMETER);
        $this->load->model("fly_search/fly_search_result_filter_values");
        $this->fly_search_result_filter_values->setFilterResultValues($combined_air_solutions);
	$search_criteria = $session->get(Fly_Constant::SESSION_SEARCH_CRITERIA_PARAMETER);
	$data["search_criteria"] = $search_criteria;
	$data['results'] = $results;
        $data[Fly_Constant::FLY_SEARCH_RESULT_FILTER_VALUES_PARAMAMETER] =  $this->fly_search_result_filter_values;
        $data["combined_air_solutions_array"] = $combined_air_solutions;
	$data['js'] = $jsfiles;
        $data['css'] = $cssfiles;
	$this->load->view("main_header",$data);
	$this->load->view("fly_result/fly_result2", $data);
	$this->load->view("main_footer", $data);
    }

    public function searchNavDay(){
         
        $action = $this->input->post("action");
        $this->load->model("fly_search/fly_search");
        $this->fly_search->searchNavDay($action);
        $response = $this->fly_search->getFlightSearchResult();
        $responseJson = json_encode($response);
	echo $responseJson;
        return;
        
    }

    public function test() {
        $data = array();
	$this->load->view("main_header",$data);
        $this->load->view("test");
        $this->load->view("main_footer", $data);
    }
    
   public function applyFilter(){
       $fiter_type = $this->input->post(Fly_Constant::FILTER_TYPE_PARAMETER);
       $filterCriteria = $this->input->post(Fly_Constant::FILTER_CRITERIA_PARAMETER);
       
       $filterCriteriaObject = json_decode($filterCriteria);
       $this->load->model("fly_search/fly_search");
       $filtered_combined_solutions = $this->fly_search->applyFilter($filterCriteriaObject);
       $this->load->model("common/ajax_response");
       $this->ajax_response->data = $filtered_combined_solutions;
       $this->ajax_response->error_type = Fly_Constant::INFO_TYPE;
       $this->ajax_response->error_code = Fly_Constant::SUCCESS_ERROR_CODE;
       $ajaxResponseArray = $this->ajax_response->getResponseArray();
       header('Content-type: application/json');
       //echo json_encode($filterCriteriaObject);
       echo json_encode($ajaxResponseArray);
       return;
       
       
   }

}

?>
